package com.whc.gulimall.member.inteceptor;

import com.whc.common.constant.SocialLoginConstant;
import com.whc.common.vo.SocialResponseVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义订单模块拦截器
 *
 * @author angaoen
 */
@Component
public class MemberInterceptor implements HandlerInterceptor {

    public static ThreadLocal<SocialResponseVo> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (new AntPathMatcher().match("/member/**", request.getRequestURI())) {
            return true;
        }

        SocialResponseVo attribute = (SocialResponseVo) request.getSession()
                .getAttribute(SocialLoginConstant.SOCIAL_LOGIN_CONSTANT);
        if (attribute != null) {
            // 已登录
            threadLocal.set(attribute);
            return true;
        } else {
            // 未登录
            request.getSession().setAttribute("msg", "请先登录");
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        }
    }
}
